package com.itheima.threadtest6_1;


import java.util.ArrayList;
import java.util.Collections;

public class MyThread extends Thread{

    //创建临时变量存储个抽奖箱的内容
    ArrayList<Integer> list ;
    static ArrayList<Integer> list1 = new ArrayList<>() ;
    static ArrayList<Integer> list2 = new ArrayList<>() ;

    public MyThread(ArrayList<Integer> list) {
        this.list = list;
    }

    public void run() {
        while(true) {
            synchronized (MyThread.class) {
                if (list.isEmpty()) {
                    // 抽奖结束
                    if ("抽奖箱1".equals(Thread.currentThread().getName())) {
                        System.out.println("线程1：" + list1);
                    } else {
                        System.out.println("线程2：" + list2);
                    }
                    break ;
                } else {
                    Collections.shuffle(list);
                    int prize = list.remove(0) ;
                    if (this.getName().equals("抽奖箱1")) {
                        list1.add(prize) ;
                    } else {
                        list2.add(prize) ;
                    }
                }
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
